﻿using LiveCharts;
using LiveCharts.Wpf;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace ICAM.Converter
{
    public class IndexConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string indexString = string.Empty;
            if (value != null && value != System.Windows.DependencyProperty.UnsetValue)
            {
                int index;
                if (int.TryParse(value.ToString(), out index))
                {
                    indexString = (index + 1).ToString();
                }
            }
            return indexString;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class ItemEndConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && value != System.Windows.DependencyProperty.UnsetValue
                && parameter != null && parameter != System.Windows.DependencyProperty.UnsetValue)
            {
                int index;
                int count;
                if (int.TryParse(value.ToString(), out index) && int.TryParse(parameter.ToString(), out count))
                {
                    return index == (count - 1);
                }
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class LevelVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && value != System.Windows.DependencyProperty.UnsetValue
                && parameter != null && parameter != System.Windows.DependencyProperty.UnsetValue)
            {
                try
                {
                    bool eq = (value.ToString() == parameter.ToString());
                    return eq ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
                }
                catch (Exception ex)
                {

                }
            }
            return System.Windows.Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class VisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && value != System.Windows.DependencyProperty.UnsetValue)
            {
                bool reverse = false;
                try
                {
                    reverse = bool.Parse(parameter.ToString());
                }
                catch
                {

                }
                try
                {
                    var vis = bool.Parse(value.ToString());
                    if (reverse)
                    {
                        return vis ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
                    }
                    else
                    {
                        return vis ? System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible;
                    }
                }
                catch (Exception ex)
                {

                }
            }
            return System.Windows.Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class MessageVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && value != System.Windows.DependencyProperty.UnsetValue)
            {

                try
                {
                    bool isEmpty = string.IsNullOrEmpty(value.ToString());
                    if (!isEmpty)
                    {
                        return System.Windows.Visibility.Visible;
                    }
                }
                catch
                {

                }
            }
            return System.Windows.Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class SumConverter : IValueConverter
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var v = value as TooltipData;

            if (v == null) return null;

            if (v.SelectionMode == TooltipSelectionMode.OnlySender) return string.Empty;

            var dd = v.Points.FirstOrDefault(t => t.ChartPoint.X == v.SharedValue);
            return dd.ChartPoint.Sum;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class ItemIndexSumConverter : IMultiValueConverter
    {
        public T GetParentObject<T>(DependencyObject obj) where T : FrameworkElement
        {
            DependencyObject parent = VisualTreeHelper.GetParent(obj);

            while (parent != null)
            {
                if (parent is T)
                {
                    return (T)parent;
                }

                parent = VisualTreeHelper.GetParent(parent);
            }

            return null;
        }
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                var item = values[1] as System.Windows.Controls.ListBoxItem;
                var items = GetParentObject<ListBox>(item);
                return int.Parse(values[0].ToString()) == items.Items.Count;
            }
            catch
            {

            }
            return false;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            string[] splitValues = ((string)value).Split(' ');
            return splitValues;
        }
    }

    public class TimeConverter : IValueConverter
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                DateTime itemTime = DateTime.Parse(value.ToString());
                TimeSpan subSpan = DateTime.Now - itemTime;

                string timeStr = "";
                if (subSpan.Days > 0)
                {
                    timeStr = $"{subSpan.Days}天前";
                    return timeStr;
                }
                else if (subSpan.Hours > 0)
                {
                    timeStr = $"{subSpan.Hours}小时前";
                    return timeStr;
                }
                else if (subSpan.Minutes > 0)
                {
                    timeStr = $"{subSpan.Minutes}分钟前";
                    return timeStr;
                }
                else
                {
                    return "刚刚发生";
                }
            }
            catch
            {

            }
            return "刚刚发生";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
